<?php

namespace App\Models;

use App\Http\Constants\Account\CommonCoinCoinConstants;
use App\Http\Constants\Account\CommonCoinReserveCoinLogConstants;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class CommonCoinCoinLog extends Model
{
    use HasFactory;

    protected $table = 'common_coin_coin_log';
    protected $appends = ['child_type_name'];

    protected $fillable = [
        'id',
        'coin_id',
        'now_coin',
        'coin',
        'child_type',
        'order_no',
        'create_time',
        'remark',
        'is_show',
        'v_create_date',
        'operator',
    ];
    protected $casts = [
        'order_no' => 'string',
    ];

    protected function getChildTypeNameAttribute()
    {
        $attributes = $this->attributes;
        $value = array_key_exists('child_type', $attributes) ? $attributes['child_type'] : null;
        if (is_null($value)) return null;
        $map = array_column(CommonCoinReserveCoinLogConstants::COMMONCOIN_TYPE, 'label', 'value');
        return array_key_exists($value, $map) ? $map[$value] : null;
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function CommonCoinCoin()
    {
        return $this->belongsTo(CommonCoinCoin::class,'coin_id','id');
    }
}
